package com.tqjc.area.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.area.entity.Stock;
import com.tqjc.area.mapper.StockMapper;
import com.tqjc.area.service.StockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 垛位信息服务实现类
 * @author: pzy
 * @date: 2023年05月17日 15:23
 */

@Service
@DS("baotou")
public class StockServiceImpl extends ServiceImpl<StockMapper,Stock> implements StockService {
    @Resource
    private StockMapper stockMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Stock queryById(Integer id) {
        return stockMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param stock   筛选条件
     * @param current 当前页码
     * @param size    每页大小
     * @return
     */
    @Override
    public Page<Stock> paginQuery(Stock stock, long current, long size) {
        //1. 构建动态查询条件
        LambdaQueryWrapper<Stock> queryWrapper = new LambdaQueryWrapper<>();
        //2. 执行分页查询
        Page<Stock> pagin = new Page<>(current, size, true);
        IPage<Stock> selectResult = stockMapper.selectByPage(pagin, queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param stock 实例对象
     * @return 实例对象
     */
    @Override
    public Stock insert(Stock stock) {
        stockMapper.insert(stock);
        return stock;
    }

    /**
     * 更新数据
     *
     * @param stock 实例对象
     * @return 实例对象
     */
    @Override
    public Stock update(Stock stock) {
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<Stock> chainWrapper = new LambdaUpdateChainWrapper<Stock>(stockMapper);
        //2. 设置主键，并更新
        chainWrapper.set(Stock::getId, stock.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if (ret) {
            return queryById(stock.getId());
        } else {
            return stock;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        int total = stockMapper.deleteById(id);
        return total > 0;
    }

    @Override
    public List<Stock> selectRollerByRollerId(Integer... rollerIds) {
        return stockMapper.selectRollerByRollerId(rollerIds);
    }

    @Override
    public Integer getStockHeightByStockId(Stock stock) {
        Integer height = stockMapper.getStockHeightByStockId("stock_"+stock.getId());
        if(height!=null){
            return height;
        }
        return 0;
    }

    @Override
    public List<Stock> getStocksByRollerIds(Integer... rollerIds){
        return stockMapper.getStocksByRollerIds(rollerIds);
    }
}
